<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎使用校园二手交易系统</title>

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
            href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
            rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">

    <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入detail样式 -->
    <link rel="stylesheet" href="css/detail.css">
    <link rel="stylesheet" href="css/order.css">
</head>
<script type="text/javascript" src="../js/jquery.min.js" ></script>

<body onload="init(1)">

<div id="wrapper">

    <!-- Sidebar -->
    <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

        <a class="sidebar-brand d-flex align-items-center justify-content-center" href="main.html">
            <div class="sidebar-brand-icon rotate-n-15">
                <i class="fas fa-laptop-house"></i>
            </div>
            <div class="sidebar-brand-text mx-3" style="font-size: 19px;">校园二手交易</div>
        </a>

        <hr class="sidebar-divider my-0">

        <!-- 返回 -->
        <li class="nav-item">
            <a class="nav-link" href="javascript:history.back()">
                <i style="font-size: 18px" class="fas fa-fw fa-arrow-left"></i>
                <span style="font-size: 20px; font-weight: bold">返回</span>
            </a>
        </li>

        <!-- Divider -->
        <hr class="sidebar-divider d-none d-md-block">

        <!-- Sidebar Toggler (Sidebar) -->
        <div class="text-center d-none d-md-inline">
            <button class="rounded-circle border-0" id="sidebarToggle"></button>
        </div>

    </ul>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">

        <!-- Main Content -->
        <div id="content">

            <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
                <div style="margin-left: 15px">
                    以下是该订单的详情，请按需要进行操作
                </div>
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                    <i class="fa fa-bars"></i>
                </button>

                <!-- Topbar Navbar -->
                <ul class="navbar-nav ml-auto">

                    <li class="nav-item dropdown no-arrow">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="mr-2 d-none d-lg-inline text-gray-600 small">
                                        欢迎您，
                                        <input type="text" id="wecomeName" style="width: 80px; border-style: none" disabled>
                                </span>
                            <img class="img-profile rounded-circle" id="sculpture"
                                 src="img/undraw_profile.svg">
                        </a>
                        <!-- Dropdown - User Information -->
                        <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
                             aria-labelledby="userDropdown">

                            <a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
                                <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
                                退出登录
                            </a>
                        </div>
                    </li>

                </ul>

            </nav>
            <!-- 详情页内容部分start -->
            <div class="de_container w">
                <div class="main">
                    <!--用户信息-->
                    <div class="pannel user_msg">
                        买家&ensp;<div class="location">
                            <img id="buyPhoto" style="width: 30px;height: 30px" class="img-profile rounded-circle"/>
                        </div>
                        <div class="user">
                            <div class="top"><h6 id="buyAccount"></h6>
                                <p id="buyName"></p>
                            </div>
                        </div>
                        <div class="more">
                            <i class="iconfont icon-more"></i>
                        </div>
                    </div>
                    <div class="pannel user_msg">
                        卖家&ensp;<div class="location">
                        <img id="sellPhoto" style="width: 30px;height: 30px" class="img-profile rounded-circle"/>
                        </div>
                        <div class="user">
                            <div class="top"><h6 id="sellAccount"></h6>
                            <p id="sellName"></p>
                        </div>
                            <div class="bottom" style="margin-left: 600px"id="address"></div>
                        </div>
                        <div class="more">
                            <i class="iconfont icon-more"></i>
                        </div>
                    </div>
                </div>
                <!--商品信息-->
                <div class="pannel goods">
                    <div class="pic">
                        <img alt="" id="photo" style="width: 75px; height: 75px">
                    </div>
                    <div class="info">
                        <h5 id="title"></h5>
                        <div class="price">
                            <span class="red">￥<i id="price"></i></span>
                        </div>
                    </div>

                </div>
                <!--配送方式-->
                <section class="pannel rest">
                    <div>
                        <h5>交易方式</h5>
                        <div id="method"></div>
                        <p id="methodStr"></p>
                    </div>
                    <div>
                        <h5>买家备注</h5>
                        <div id="remark"></div>
                        <p id="rethodStr"></p>
                    </div>
                </section>
                <!--底部支付-->
                <div class="pay">
                    <div class="left">
                        商品价格：<span class="red">￥<i id="price2"></i></span>
                    </div>
                    <div style="margin-left: 800px" id="status">
                    </div>
                    <div class="right" id="button">
                    </div>
                    <div class="right" id="buttonCancel">
                    </div>
                </div>
                </br>
                <div style="display: none; height: 200px" id="hiddenDiv" class="pay" >
                    好评&ensp;&ensp;<input type="radio" name="rating" id="great" value="1"/>&ensp;&ensp;
                    中评&ensp;&ensp;<input type="radio" name="rating" id="mid"value="2"/>&ensp;&ensp;
                    差评&ensp;&ensp;<input type="radio" name="rating" id="bad"value="3"/>&ensp;&ensp;
                    </br>
                    </br>
                    <textarea id="evaluationContext" name="evalutionContext" class="text-area" placeholder="请输入您对本次交易的评价" style="height: 100px"></textarea>
                    <input type="button" value="提交" onclick="evaluation()"  style="text-align: center; width: 150px; font-size: 18px" class="btn btn-primary">
                </div>
            </div>
        </div>

    </div>
</div>
<!--        底部-->
<footer class="sticky-footer bg-white">
    <div class="container my-auto">
        <div class="copyright text-center my-auto">
            <span>致 力 于 打 造 最 方 便 安 全 的 校 园 二 手 交 易 服 务</span>
        </div>
    </div>
</footer>

<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
     aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">确定要退出登录？</h5>
                <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">点击‘退出’后，退出系统...</div>
            <div class="modal-footer">
                <button class="btn btn-secondary" type="button" data-dismiss="modal">取消</button>
                <a class="btn btn-primary" href="login.html">退出</a>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap core JavaScript-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>

<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>

<!-- Page level plugins -->
<script src="vendor/chart.js/Chart.min.js"></script>

<!-- Page level custom scripts -->
<script src="js/demo/chart-area-demo.js"></script>
<script src="js/demo/chart-pie-demo.js"></script>


</body>
<script>
    userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
    if(userInfo.userName != null){
        document.getElementById('wecomeName').value = userInfo.userName; // 设置用户名
    }
    $("#sculpture").attr("src", userInfo.photo);  // 设置头像
    orderId = sessionStorage.getItem("orderId");
    var sellId;
    var goodsId;
    var price;
    function init() {
        $.ajax({
            type: "GET",
            async: false,
            url: "order/order-server/get",
            headers: { // token
                'Authorization': userInfo.token
            },
            dataType: "json",        //返回数据形式为json
            data: {"id": orderId},
            success: function (result) {
                // console.log(result);//打印服务端返回的数据(调试用)
                if (result.status == 200) {
                    document.getElementById('sellName').innerHTML = result.data.sellName;
                    document.getElementById('sellAccount').innerHTML = result.data.sellAccount;
                    document.getElementById('address').innerHTML = result.data.address;
                    document.getElementById('buyName').innerHTML = result.data.buyName;
                    document.getElementById('buyAccount').innerHTML = result.data.buyAccount;
                    document.getElementById('title').innerHTML = result.data.title;
                    document.getElementById('price').innerHTML = result.data.price;
                    document.getElementById('price2').innerHTML = result.data.price;
                    document.getElementById('sellPhoto').src = result.data.sellPhoto;
                    document.getElementById('buyPhoto').src = result.data.buyPhoto;
                    document.getElementById('photo').src = result.data.photo;
                    sellId = result.data.sellId;
                    goodsId = result.data.goodsId;
                    price = result.data.price;
                }
                var methodStr;
                if(result.data.method == "1"){
                    methodStr="自取";
                } else if(result.data.method == "2"){
                    methodStr="送货上门";
                } else if(result.data.method == "3"){
                    methodStr="约定地点交货";
                }
                // 初始化交易方式和备注，支付前可以修改
                var methodRadio = "";
                if(result.data.status == "1"){
                    document.getElementById('status').innerHTML = "待支付";
                    var methods = result.data.methods;
                    if(result.data.buyId == userInfo.userId){ // 用户是买家
                        if(methods.indexOf("1") != -1){
                            methodRadio += "&ensp;&ensp;自取<input type='radio' name='method' value='1'>";
                        }
                        if(methods.indexOf("2") != -1){
                            methodRadio += "&ensp;&ensp;送货上门<input type='radio' name='method' value='2'>";
                        }
                        if(methods.indexOf("3") != -1){
                            methodRadio += "&ensp;&ensp;约定地点交货<input type='radio' name='method' value='3'>";
                        }
                        document.getElementById('method').innerHTML = methodRadio;
                        document.getElementById('button').innerHTML = "<a href=\"javascript:update(2)\">支付</a>";
                        document.getElementById('remark').innerHTML = "&ensp;&ensp;<input type='text' id='remarkInput' style='width: 800px'>";
                    }
                    document.getElementById('buttonCancel').innerHTML = "<a href=\"javascript:cancel('" + result.data.sellId + "')\">取消订单</a>";
                } else if(result.data.status == "2"){
                    document.getElementById('status').innerHTML = "已支付";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                    if(result.data.buyId == userInfo.userId) {
                        document.getElementById('button').innerHTML = "<a href=\"javascript:update(3)\">确认收货</a>";
                    }
                    document.getElementById('buttonCancel').innerHTML = "<a href=\"javascript:cancel('" + result.data.sellId + "')\">取消订单</a>";
                } else if(result.data.status == "3"){
                    document.getElementById('status').innerHTML = "订单完成";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                    if(result.data.buyId == userInfo.userId) { // 是买家
                        document.getElementById("hiddenDiv").style.display = "block"; // 显示评论界面
                    }
                } else if(result.data.status == "4"){
                    document.getElementById('status').innerHTML = "退款中";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                    if(result.data.sellId == userInfo.userId){ // 用户是卖家
                        document.getElementById('button').innerHTML = "<a href=\"javascript:update(5)\">同意取消</a>";
                    }
                } else if(result.data.status == "5"){
                    document.getElementById('status').innerHTML = "买家取消";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                } else if(result.data.status == "6"){
                    document.getElementById('status').innerHTML = "卖家取消";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                } else if(result.data.status == "7"){
                    document.getElementById('status').innerHTML = "订单失效";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                } else if(result.data.status == "8"){
                    document.getElementById('status').innerHTML = "订单完成";
                    document.getElementById('methodStr').innerHTML = methodStr;
                    document.getElementById('rethodStr').innerHTML = result.data.remark;
                }
            }
        });
    }

    function cancel(sellId){
        if (sellId != userInfo.userId) {
            update(4); // 买家取消
        } else {
            update(6); // 卖家取消
        }
    }

    function update(status) {
        var flagReturn;
        var confirmStr;
        if(status == 2){
            var nodex = document.getElementsByName("method");
            var flag = false;
            for(var i=0;i<nodex.length;i++){
                //选择框不为空
                if(nodex[i].checked==true){
                    flag=true;
                }
            }
            if(!flag){
                alert("请选择交易方式");
                return;
            }
            $.ajax({
                type: "GET",
                async: false,
                url: "goods/goods-server/get",
                headers: { // token
                    'Authorization': userInfo.token
                },
                dataType: "json",        //返回数据形式为json
                data: {"id": goodsId},
                success: function (result) {
                    if (result.status == 200) {
                        if(result.data.status == 5){ // 商品在付款前状态变为已售出
                            alert("抱歉！该商品已被购买");
                            status = 7; // 订单失效
                        } else if(result.data.status == 2){
                            var balanceParams={
                                id : userInfo.userId,
                                status : 2,
                                amount : price
                            }
                            $.ajax({
                                type: "PUT",
                                async: false,
                                url: "user/user-server/updateBalance",
                                headers: { // token
                                    'Authorization' : userInfo.token
                                },
                                dataType: "json",        //返回数据形式为json
                                contentType: 'application/json; charset=UTF-8',
                                data: JSON.stringify(balanceParams),
                                success: function (result) {
                                    if (result.status == 200) {
                                    } else{
                                        alert(result.msg);
                                        flagReturn = 1;
                                    }
                                }
                            });
                        }
                    }
                }
            });
            if(flagReturn == 1){
                return ;
            }
            var goodsInfoParams = {
                id : goodsId,
                status : 5
            }
            $.ajax({
                url: 'goods/goods-server/update',
                method: 'PUT',
                headers: { // token
                    'Authorization': userInfo.token
                },
                dataType: 'json',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(goodsInfoParams),
                success: function (result) {
                }
            })
            confirmStr = "支付";
        } else if( status == 3){
            confirmStr = "确认收货";
        } else if( status == 5){
            confirmStr = "同意取消";
        } else{
            confirmStr = "取消订单";
        }

        var sure;
        if(status != 7 && status != 8){
            sure = confirm("您确定要" + confirmStr + "吗?");
        } else {
            sure = true;
        }
        if (sure) {
            var orderParams = {
                id : orderId,
                status : status,
                method : $("input[name='method']:checked").val(),
                remark : $("#remarkInput").val()
            }
            $.ajax({
                url: 'order/order-server/update',
                method: 'PUT',
                headers: { // token
                    'Authorization': userInfo.token
                },
                dataType: 'json',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(orderParams),
                success: function (result) {
                    if (result.status == 200) {
                        if(status != 7){
                            alert("操作成功");
                        }
                        window.location.href = 'orderDetail.html';
                    } else {
                        alert(result.msg);
                    }
                }
            })
        }
    }
    function evaluation() {
        var evaluationParams={
            postId : userInfo.userId,
            getId : sellId,
            orderId : orderId,
            context : $("#evaluationContext").val(),
            rating : $("input[name='rating']:checked").val(),
        }
        $.ajax({
            type : "PUT",
            async: false,
            url : "evaluation/evaluation-server/insert",
            headers: { // token
                'Authorization' : userInfo.token
            },
            dataType: "json",        //返回数据形式为json
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(evaluationParams),
            success: function (result) {
                if (result.status == 200) {
                    alert("评论成功");
                    update(8);
                    document.getElementById("hiddenDiv").style.display = "block";
                } else{
                    alert(result.msg);
                }
            }
        });
    }

</script>

</html>